import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:${USER}
 * Date:${YEAR}-${MONTH}-${DAY}
 * ${TIME}
 */
public class Main {
   public class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        if(root==null){
            return new LinkedList<>();
        }
        Queue<TreeNode> queue=new LinkedList<>();
        List<List<Integer>> lists=new ArrayList<>();
        queue.add(root);
        boolean b=false;
        while (!queue.isEmpty()){
            List<Integer> ret=new ArrayList<>();
            int sz=queue.size();
            for(int i=0;i<sz;i++){
                TreeNode node=queue.poll();
                ret.add(node.val);
                if(node.left!=null){
                    queue.offer(node.left);
                }
                if(node.right!=null){
                    queue.offer(node.right);
                }

            }
            if(b==true){
                Collections.reverse(ret);
                b=false;
            }else{
                b=true;
            }
            lists.add(ret);
        }
        return lists;
    }
}